﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using MyToDo.Common;
using MyToDo.Common.Models;
using MyToDo.Extensions;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;

namespace MyToDo.ViewModels
{
    class MainViewModel : BindableBase,IConfigureService
    {
        public MainViewModel(IRegionManager regionManager,IContainerProvider containerProvider)
        {
            MenuBars = new ObservableCollection<MenuBar>();
           
            NavigateCommand = new DelegateCommand<MenuBar>(Navigate);
            this.regionManager = regionManager;
            GoBackCommand = new DelegateCommand(() => {
                if (journal != null &&  journal.CanGoBack)
                {
                    journal.GoBack();
                }
            });
            GoForwardCommand = new DelegateCommand(() => {
                if (journal != null && journal.CanGoForward)
                {
                    journal.GoForward();
                }
            });

            LoginOutCommand = new DelegateCommand(() => {
                App.LoginOut(containerProvider);
            });
        }

        private void Navigate(MenuBar obj)
        {
            if (obj == null || string.IsNullOrWhiteSpace(obj.NameSpace))
                return;

            regionManager
                .Regions[PrismManager.MainViewRegionName]
                .RequestNavigate(
                    obj.NameSpace,
                    back =>
                    {
                        journal = back.Context.NavigationService.Journal;
                    }
                );
           
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }

        public DelegateCommand<MenuBar> NavigateCommand { get; private set; }


        public DelegateCommand LoginOutCommand { get; private set; }

        public DelegateCommand GoBackCommand { get; private set; }
        public DelegateCommand GoForwardCommand { get; private set; }

        private ObservableCollection<MenuBar> menuBars;
        private readonly IRegionManager regionManager;
        private IRegionNavigationJournal journal;
        public ObservableCollection<MenuBar> MenuBars
        {
            get { return menuBars; }
            set
            {
                menuBars = value;
                RaisePropertyChanged();
            }
        }

        void CreateMenuBar()
        {
            menuBars.Add(
                new MenuBar()
                {
                    Icon = "Home",
                    Title = "首页",
                    NameSpace = "IndexView",
                }
            );
            menuBars.Add(
                new MenuBar()
                {
                    Icon = "NotebookOutline",
                    Title = "待办事项",
                    NameSpace = "ToDoView",
                }
            );
            menuBars.Add(
                new MenuBar()
                {
                    Icon = "NotebookPlus",
                    Title = "备忘录",
                    NameSpace = "MemoView",
                }
            );
            menuBars.Add(
                new MenuBar()
                {
                    Icon = "Cog",
                    Title = "设置",
                    NameSpace = "SettingView",
                }
            );
        }
        /// <summary>
        /// 配置首页初始化
        /// </summary>
        public void Configure()
        {
            UserName = AppSession.UserName;
            CreateMenuBar();
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView");
        }
    }
}
